package cn.edu.huanghuai.newsofhuanghuai.Controller;

import cn.edu.huanghuai.newsofhuanghuai.pojo.News;
import cn.edu.huanghuai.newsofhuanghuai.sevice.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import cn.edu.huanghuai.newsofhuanghuai.common.Result;

import java.util.List;

/**
 * @user:whisper
 * @date:6-11
 * @description:
 */

@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {


    @Autowired
    private NewsService newsService;



    @PostMapping("/add")
    public Result addNews(@RequestBody  News news){
        int result = newsService.addNews(news);
        return result >  0 ? new Result(200,"信息新增成功") : new Result(403,"信息新增失败");
    }

    @GetMapping("/delete/{nid}")
    public Result deleteNews(@PathVariable Integer nid){
        int result = newsService.deleteNews(nid);
        return result >  0 ? new Result(200,"信息删除成功") : new Result(403,"信息删除失败");


    }
    @PostMapping("/update")
    public Result updateNews(@RequestBody News news){
        int result = newsService.updateNews(news);
        return result >  0 ? new Result(200,"信息更新成功") : new Result(403,"信息更新失败");


    }

    /**
     * 根据新闻的id去查询具体的新闻信息
     * @param nid
     * @return
     */
    @GetMapping("/query/{nid}")
    public Result queryNews(@PathVariable Integer nid){
        News news = newsService.queryNews(nid);
        return news != null ? new Result(200,"信息查询成功",news) : new Result(403,"信息查询失败");


    }

    @GetMapping("/queryAll")
    public Result queryAllNews(){
        List<News> news = newsService.queryAllNews();
        return news != null ? new Result(200,"信息查询成功",news) : new Result(403,"信息查询失败");
    }


    /**
     * 根据文章的标题模糊查询文章
     * @param title
     * @return
     */
    @GetMapping("/queryByTitleName")
    public Result queryByTitleName(String title){
        // 注意 这里模糊查询 需要的是 % %

        return new Result(200, "查询成功", newsService.queryNewsByTitle("%" + title + "%"));
    }

}
